package net.pws.oos.web.dto;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import net.pws.common.beanutils.BeanUtils;
import net.pws.common.persistence.EntityManager;
import net.pws.oos.biz.model.TaskTemplate;

public class TaskTemplateDto extends TaskTemplate {

	private static final long serialVersionUID = -1202102601133556505L;

	public static TaskTemplateDto from(TaskTemplate model) {
		String[] ignoredProps = { "parent" };
		TaskTemplateDto dto = new TaskTemplateDto();
		BeanUtils.copyProperties(dto, model, ignoredProps);
		return dto;
	}

	public static List<TaskTemplateDto> from(List<TaskTemplate> models) {
		List<TaskTemplateDto> dtos = new ArrayList<TaskTemplateDto>();
		for (TaskTemplate model : models) {
			dtos.add(from(model));
		}
		return dtos;
	}

	public static TaskTemplate to(TaskTemplate model, TaskTemplateDto dto,
			EntityManager entityManager) {
		if (model == null) {
			model = new TaskTemplate();
		}
		String[] ignoredProps = { "parent", "createDate" };
		BeanUtils.copyProperties(model, dto, ignoredProps);
		if (StringUtils.isNotBlank(dto.getParentId())) {
			TaskTemplate parent = entityManager.getReference(TaskTemplate.class, dto.getParentId());
			model.setParent(parent);
		}
		return model;
	}

	private String parentId;

	private String parentName;
	
	private int childCount;

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public String getParentName() {
		return parentName;
	}

	public void setParentName(String parentName) {
		this.parentName = parentName;
	}

	public int getChildCount() {
		return childCount;
	}

	public void setChildCount(int childCount) {
		this.childCount = childCount;
	}

}
